﻿using System.Windows;
using System.Windows.Controls;
using PwdMgrUI.Utils;
using PwdMgr.PwdMgrService.DataService;
using PwdMgr.CommonContract;
using PwdMgr.PwdMgrService.Sessions;
using System;

namespace PwdMgrUI
{
    /// <summary>
    /// Interaction logic for EditSensitiveInfo.xaml
    /// </summary>
    public partial class AddSensitiveInfo : UserControl
    {
        public AddSensitiveInfo()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(EditSensitiveInfo_Loaded);
            this.Unloaded += new RoutedEventHandler(EditSensitiveInfo_Unloaded);
        }

        void EditSensitiveInfo_Unloaded(object sender, RoutedEventArgs e)
        {
        }

        private IDataService _dataService;

        private IDataService DataService
        {
            get 
            {
                if(_dataService == null)
                    _dataService = DataServiceFactory.CreateInstance();
                return _dataService;
            }
        }

        void EditSensitiveInfo_Loaded(object sender, RoutedEventArgs e)
        {
            this.btn_OK.Click += new RoutedEventHandler(Btn_OK_Click);
            this.btn_GC.Click += new RoutedEventHandler(btn_GC_Click);
        }

        void btn_GC_Click(object sender, RoutedEventArgs e)
        {
            GC.Collect();
        }

        void Btn_OK_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.tbox_Username.Text))
            {
                UIAlert.Error("Username couldn't be null.");
                return;
            }

            if (string.IsNullOrEmpty(this.pbox_Password.Password))
            {
                UIAlert.Error("Password couldn't be null.");
                return;
            }

            if (string.IsNullOrEmpty(this.tbox_Tags.Text))
            {
                UIAlert.Error("Tags couldn't be null.");
                return;
            }

            Session session = App.GetSession();
            if (session == null)
            {
                UIAlert.Error("Please login.");
                return;
            }

            SensitiveInfo info = new SensitiveInfo
            {
                Username = this.tbox_Username.Text,
                Password = this.pbox_Password.Password,
            };

            SplitTags(this.tbox_Tags.Text, info);

            if (string.IsNullOrWhiteSpace(info.Tag1))
            {
                UIAlert.Error("Invalid tag.");
                return;
            }

            try
            {
                DataService.CreateSensitiveInfo(info, session.SessionID);
                OnAddCompleted();
            }
            catch (Exception ex)
            {
                UIAlert.Error(ex.ToString());
            }
        }

        private void SplitTags(string inputTag, SensitiveInfo info)
        {
            if (inputTag == null || info == null)
                throw new ArgumentNullException();

            int index = 0;
            string[] splits = inputTag.Split(';', '；');
            for (; index < splits.Length; index++)
            {
                if (!string.IsNullOrWhiteSpace(splits[index]))
                {
                    info.Tag1 = splits[index++];
                    break;
                }
            }

            for (; index < splits.Length; index++)
            {
                if (!string.IsNullOrWhiteSpace(splits[index]))
                {
                    info.Tag2 = splits[index++];
                    break;
                }
            }

            for (; index < splits.Length; index++)
            {
                if (!string.IsNullOrWhiteSpace(splits[index]))
                {
                    info.Tag3 = splits[index++];
                    break;
                }
            }

            for (; index < splits.Length; index++)
            {
                if (!string.IsNullOrWhiteSpace(splits[index]))
                {
                    info.Tag4 = splits[index++];
                    break;
                }
            }

            for (; index < splits.Length; index++)
            {
                if (!string.IsNullOrWhiteSpace(splits[index]))
                {
                    info.Tag5 = splits[index++];
                    break;
                }
            }
        }
    }
}
